package net.loonggg.mvpdemo.presenter;

import net.loonggg.mvpdemo.bean.User;
import net.loonggg.mvpdemo.model.GetUserInfo;
import net.loonggg.mvpdemo.model.IGetUser;
import net.loonggg.mvpdemo.model.OnUserInfoListener;
import net.loonggg.mvpdemo.view.IShowUserView;
import android.os.Handler;

public class UserInfoPresenter {    
    private IGetUser iGetUser;    
    private IShowUserView iShowUserView;    
    private Handler mHandler = new Handler();    
    public UserInfoPresenter(IShowUserView iShowUserView) {        
        this.iShowUserView = iShowUserView;        
        this.iGetUser = new GetUserInfo();
    }    
    
    public void getUserInfoToShow(int id) {
        iShowUserView.showLoading();
        iGetUser.getUserInfo(id, new OnUserInfoListener() {            @Override
            public void getUserInfoSuccess(final User user) {  // 需要在UI线程执行
                mHandler.post(new Runnable() {                    @Override
                    public void run() {
                        iShowUserView.toMainActivity(user);
                        iShowUserView.hideLoading();
                    }
                });
            }            @Override
            public void getUserInfoFailed() {
                mHandler.post(new Runnable() {                    @Override
                    public void run() {
                        iShowUserView.showFailedError();
                        iShowUserView.hideLoading();
                    }
                });
            }
        });
    }

}